/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.web.dao.support.hbn.cfg;

import org.hibernate.AssertionFailure;
import org.hibernate.cfg.ImprovedNamingStrategy;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.util.StringHelper;

import java.util.Locale;

/**
 *
 * hibernate 命名策略
 * 可通过
 * 1. hibernate.cfg.xml 中配置
 * <property name="ejb.naming_strategy">org.vacoor.nothing.web.dao.support.hbn.cfg.UpperCaseUnderscoreNamingStrategy</property>
 * <property name="hibernate.ejb.naming_strategy">org.vacoor.nothing.web.dao.support.hbn.cfg.UpperCaseUnderscoreNamingStrategy</property>
 * 2. Configuration
 *  config.setNamingStrategy(UpperCaseUnderscoreNamingStrategy.INSTANCE)
 *
 * @author vacoor
 */
public class UpperCaseUnderscoreNamingStrategy extends ImprovedNamingStrategy {
    public static final NamingStrategy INSTANCE = new UpperCaseUnderscoreNamingStrategy();

    @Override
    public String classToTableName(String className) {
        return super.classToTableName(className).toUpperCase(Locale.ENGLISH);
    }

    @Override
    public String propertyToColumnName(String propertyName) {
        return super.propertyToColumnName(propertyName).toUpperCase(Locale.ENGLISH);
    }

    @Override
    public String tableName(String tableName) {
        return super.tableName(tableName).toUpperCase(Locale.ENGLISH);
    }

    @Override
    public String columnName(String columnName) {
        return super.columnName(columnName).toUpperCase(Locale.ENGLISH);
    }

    @Override
    public String collectionTableName(String ownerEntity, String ownerEntityTable, String associatedEntity, String associatedEntityTable, String propertyName) {
        return super.collectionTableName(ownerEntity, ownerEntityTable, associatedEntity, associatedEntityTable, propertyName).toUpperCase(Locale.ENGLISH);
    }

    @Override
    public String joinKeyColumnName(String joinedColumn, String joinedTable) {
        return super.joinKeyColumnName(joinedColumn, joinedTable).toUpperCase(Locale.ENGLISH);
    }

    @Override
    public String foreignKeyColumnName(String propertyName, String propertyEntityName, String propertyTableName, String referencedColumnName) {
        // return super.foreignKeyColumnName(propertyName, propertyEntityName, propertyTableName, referencedColumnName).toUpperCase(Locale.ENGLISH);
        String header = propertyName != null ? StringHelper.unqualify(propertyName) : propertyTableName;
        if (header == null) throw new AssertionFailure("NamingStrategy not properly filled");
        return columnName(header) + "_" + referencedColumnName; // super is not used for backward compatibility
    }

    @Override
    public String logicalColumnName(String columnName, String propertyName) {
        return super.logicalColumnName(columnName, propertyName).toUpperCase(Locale.ENGLISH);
    }

    @Override
    public String logicalCollectionTableName(String tableName, String ownerEntityTable, String associatedEntityTable, String propertyName) {
        return super.logicalCollectionTableName(tableName, ownerEntityTable, associatedEntityTable, propertyName).toUpperCase(Locale.ENGLISH);
    }

    @Override
    public String logicalCollectionColumnName(String columnName, String propertyName, String referencedColumn) {
        return super.logicalCollectionColumnName(columnName, propertyName, referencedColumn).toUpperCase(Locale.ENGLISH);
    }
}
